<template>
<div class='register-page'>

    <van-nav-bar
  title="账号注册"
  left-arrow
  @click-left="onClickLeft
  "
/>

  <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="username"
    placeholder="请输入账号"
    :rules="[
    { required: true, message: '请填写用户名' },
    //添加正则校验
    {pattern:/^\w{5,}$/,message:'用户名至少为5位'}]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    placeholder="请输入密码"
    :rules="[
    { required: true, message: '请填写密码' },
     { pattern:/^\w{5,}$/,message:'密码至少为6位'}
    ]"
  />
  <div style="margin: 16px;">
    <van-button  block  type="primary"  native-type="submit">注册</van-button>
  </div>
</van-form>
<router-link class="link" to='/login'>已有账号，去登录~</router-link>

</div>
</template>

<script>
import { register } from '@/api/user'
import { Toast } from 'vant'
export default {
  name: 'register-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      await register(values)// 发送请求
      Toast.success('注册成功')// 提示
      this.$router.push('/login')// 跳转
    },
    onClickLeft () {
      // 返回上一页
      this.$router.go(-1)
    }
  }

}
</script>

<style lang="less" scoped>
.van-nav-bar__title {
color: #fff;
}
.van-icon-arrow-left:before{
  color: #fff;
}
.link {
  color: #333;
  font-size: 12px;
  text-align: center;
  margin-top: 30px;
  display: block;
}

</style>
